package com.x52im.rainbowchat.http.util;

import com.eva.epc.common.util.CommonUtils;
import com.eva.framework.utils.LoggerFactory;
import com.x52im.rainbowchat.BaseConf;
import com.x52im.rainbowchat.im.JedisDateSource;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Map;

public class JedisTools {
    public static String PRE_LOGIN_USER_KEY = "__loginUser__";  // 登录后写入的session的key

    public static String getLoginUserKey(String id) {
        return PRE_LOGIN_USER_KEY + id;
    }

    public static String PRE_SMS_FINDPSW_USER_KEY = "__smsFindpswUser__";  //  找回密码的短信缓存
    public static String PRE_SMS_REGISTER_USER_KEY = "__smsRegisterUser__";  // 注册的短信缓存
    public static String PRE_SMS_PAYPASW_USER_KEY = "__smsPayPws__";  // 设置(修改)支付密码的短信缓存

    public static String getSmsPaypswKey(String phone) {
        return PRE_SMS_PAYPASW_USER_KEY + phone;
    }

    public static String getSmsFindpswUserKey(String phone) {
        return PRE_SMS_FINDPSW_USER_KEY + phone;
    }

    public static String getSmsRegisterUserKey(String phone) {
        return PRE_SMS_REGISTER_USER_KEY + phone;
    }


    public static String PRE_RED_ENVELOPE_KEY = "__redEnvelope__";

    public static String getRedEnvelopeKeyKey(String id) {
        return PRE_RED_ENVELOPE_KEY + id;
    }


//    public static JedisPool jedisPool = null;

//    /**
//     * 获取Jedis实例
//     *
//     * @return
//     */
//    public synchronized static Jedis getResource() {
//        try {
//            LoggerFactory.getLog().debug("jedisPool----" + jedisPool.toString());
//            if (jedisPool != null) {
//                Jedis resource = jedisPool.getResource();
//                LoggerFactory.getLog().debug("jedisPool resource ----" + jedisPool.toString());
//                return resource;
//            } else {
//                return null;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    /**
     * 获取Jedis实例
     *
     * @return
     */
    public synchronized static Jedis getResource() {
        Jedis jedis = JedisDateSource.getResource();
        LoggerFactory.getLog().debug("jedis  ----" + jedis);
        return jedis;
    }

//    /**
//     * 释放jedis资源
//     *
//     * @param jedis
//     */
//    public static void returnResource(final Jedis jedis) {
//        if (jedis != null) {
//            jedisPool.returnResource(jedis);
//        }
//    }

    /**
     * 释放jedis资源
     *
     * @param jedis
     */
    public static void returnResource(final Jedis jedis) {
        if (jedis != null) {
            JedisDateSource.returnResource(jedis);
        }
    }


    /**
     * 获取redis键值-object
     *
     * @param key
     * @return
     */
    public static Object getObject(String key) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /**
     * 设置redis键值-object
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean setObject(String key, String value) {
        boolean flag = false;
        Jedis jedis = null;
        try {
            jedis = getResource();
            return "OK".equals(jedis.set(key, value));
        } catch (Exception e) {
            e.printStackTrace();
            return flag;
        } finally {
            returnResource(jedis);
        }
    }

    public static boolean setObject(String key, String value, int expiretime) {
        boolean result = false;
        Jedis jedis = null;
        try {
            jedis = getResource();
            return "OK".equals(jedis.setex(key, expiretime, value));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 删除key
     */
    public static boolean delkeyObjectDB(String key, int index) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            jedis.select(index);
            return 1 == jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            returnResource(jedis);
        }
    }

    /**
     * 删除key
     */
    public static boolean delkeyObject(String key) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            return 1 == jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            returnResource(jedis);
        }
    }

    /*存在某个Key*/
    public static Boolean existsObjectDB(String key, int index) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            jedis.select(index);
            return jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            returnResource(jedis);
        }
    }

    /*存在某个Key*/
    public static Boolean existsObject(String key) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            returnResource(jedis);
        }
    }

    /**
     * @param key
     * @param value
     * @return
     */
    public static long hset(String key, Map<String, String> value) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = getResource();
            result = jedis.hset(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 如果为空就set值，并返回1
     * 如果存在(不为空)不进行操作，并返回0
     *
     * @param key
     * @param value
     * @return
     */
    public static long setnxDB(String key, String value, int index) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = getResource();
            jedis.select(index);
            result = jedis.setnx(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /**
     * 如果为空就set值，并返回1
     * 如果存在(不为空)不进行操作，并返回0
     *
     * @param key
     * @param value
     * @return
     */
    public static long setnx(String key, String value) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = getResource();
            result = jedis.setnx(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return result;
    }

    /*设置字符串结果*/
    public static boolean setDB(String key, String value, int index) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            jedis.select(index);
            String result = jedis.set(key, value);
            if ("OK".equals(result)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return false;
    }


    /*设置字符串结果*/
    public static boolean set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            String result = jedis.set(key, value);
            if ("OK".equals(result)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return false;
    }

    /*设置字符串结果带过期时间*/
    public static boolean set(String key, String value, long expiretime) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            String result = jedis.setex(key, expiretime, value);
            if ("OK".equals(result)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return false;
    }

    /*获得字符串结果*/
    public static String get(String key) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            String result = jedis.get(key);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return null;
    }

    /*获得字符串结果*/
    public static String getDB(String key, int index) {
        Jedis jedis = null;
        try {
            jedis = getResource();
            jedis.select(index);
            String result = jedis.get(key);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            returnResource(jedis);
        }
        return null;
    }

}
